﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using AxSHDocVw;

namespace Insurance_Quotation_App
{
    public static class HSBC
    {
        private static AxWebBrowser browser = null;
        private static AxWebBrowser popup = null;

        public static void Run(AxWebBrowser axWebBrowser, AxWebBrowser axWebBrowserPopup)
        {
            browser = axWebBrowser;
            popup = axWebBrowserPopup;

            browser.DocumentComplete += new DWebBrowserEvents2_DocumentCompleteEventHandler(browser_DocumentComplete);
            browser.Navigate("http://www.insurance.hsbc.com.sg/1/2/sghi/personal-insurance/protection/autosurance");
        }

        private static void browser_DocumentComplete(object sender, DWebBrowserEvents2_DocumentCompleteEvent e)
        {
            browser.DocumentComplete -= browser_DocumentComplete;

            browser.NewWindow2 += new DWebBrowserEvents2_NewWindow2EventHandler(browser_NewWindow2);
            Autofill.ClickButton(browser, "alt", "AutoSurance Take to the road with extra peace of mind", "img");
        }

        private static void browser_NewWindow2(object sender, DWebBrowserEvents2_NewWindow2Event e)
        {
            browser.NewWindow2 -= browser_NewWindow2;
            popup.DocumentComplete += new DWebBrowserEvents2_DocumentCompleteEventHandler(popup_DocumentComplete);

            popup.RegisterAsBrowser = true;
            e.ppDisp = popup.Application;
        }

        private static void popup_DocumentComplete(object sender, DWebBrowserEvents2_DocumentCompleteEvent e)
        {
            popup.DocumentComplete -= popup_DocumentComplete;
            popup.DocumentComplete += new DWebBrowserEvents2_DocumentCompleteEventHandler(popup_DocumentComplete2);
        }

        private static void popup_DocumentComplete2(object sender, DWebBrowserEvents2_DocumentCompleteEvent e)
        {
            popup.DocumentComplete -= popup_DocumentComplete2;

            Autofill.SetSelectList(popup, "name", "cover", "Classic");
            Autofill.SetSelectList(popup, "name", "dateOfBirthDay", "7");
            Autofill.SetSelectList(popup, "name", "dateOfBirthMonth", "8");
            Autofill.SetText(popup, "name", "dateOfBirthYear", "1984");
            Autofill.SetSelectList(popup, "name", "licenseObtainedDay", Common.licDay);
            Autofill.SetSelectList(popup, "name", "licenseObtainedMonth", Common.licMonth);
            Autofill.SetText(popup, "name", "licenseObtainedYear", Common.licYear);
            Autofill.SetSelectList(popup, "name", "noClaimDiscount", "30");
            Autofill.SetSelectList(popup, "name", "usageCar", "CLASS 1A - INDOOR/OUTDOOR     ");
            Autofill.SetSelectList(popup, "name", "makeCar", "TOYOTA                        ");
            Autofill.SetText(popup, "name", "modelCar", "TOYOTA COROLLA 1.3 DX");
            Autofill.SetSelectList(popup, "name", "bodyCar", "SALOON                        ");
            Autofill.SetText(popup, "name", "capacityCar", "1290");
            Autofill.SetText(popup, "name", "yearCarRegistered", "2010");

            popup.DocumentComplete += new DWebBrowserEvents2_DocumentCompleteEventHandler(popup_DocumentComplete3);
            Autofill.ClickButton(popup, "name", "submitButton", "input");
        }

        private static void popup_DocumentComplete3(object sender, DWebBrowserEvents2_DocumentCompleteEvent e)
        {
            popup.DocumentComplete -= popup_DocumentComplete3;
            Autofill.SetRadio(popup, "name", "businessUsage", "input");

            Autofill.ClickButton(popup, "value", " Next ", "input");
        }
    }
}
